/** @class tMessage
 messaging base class
 
 $Id: tMessage.cpp 2 2012-04-04 15:07:32Z gerhardus1@gmail.com $
 Versioning: a.b.c a is a major release, b represents changes or new features, c represents bug fixes. 
 @version 1.0.0   06/10/2011    Gerhardus Muller     Script created

 @note

 @todo
 
 @bug

 Copyright Gerhardus Muller
 */

#include "message/tMessage.h"
#include "src/recoveryLog.h"

const char* tMessage::PROTOCOL_VERSION_NUMBER = "4.0";
const char* tMessage::FRAME_HEADER = "##tservpacket##v";



/******
 * serialisation / deserialisation
 * **/
void tMessage::parseBody( const char* body )
{
} // parseBody

/******
 * construction /destruction
 * **/
tMessage::tMessage( )
  : object( "tMessage" )
{
  construct();
} // tMessage

tMessage::tMessage( eEventType type, const char* queue, const char* objName )
  : object( objName )
{
  construct();
  if( queue != NULL ) destQueue = queue;
  eventType = type;
  log.setInstanceName( typeToString() );
} // tMessage

tMessage::tMessage( const char* body, const char* objName )
  : object( objName )
{
  construct();
  parseBody( body );
  log.setInstanceName( typeToString() );
} // tMessage

// seems a bit empty .. suppose the compiler was mouning somewhere
tMessage::tMessage( const tMessage& c )
  : object( c.log.getInstanceName().c_str() )
{
  construct();
} // tMessage

tMessage::~tMessage()
{
} // ~tMessage

/**
 * initialises the object from a construction perspective
 * **/
void tMessage::construct( )
{
  eventType = EV_UNKNOWN;
  expiryTime = 0;
  lifetime = -1;
  queueTime = 0;
} // construct

/******
 * string representation of the object
 * object string serialiser
 * **/
std::string tMessage::toString( )
{
  std::ostringstream oss;
  oss << typeToString();

	return oss.str();
} // toString

/**
 * terse version of toString
 * **/
std::string tMessage::toStringBrief( )
{
  std::ostringstream oss;
  oss << typeToString();

	return oss.str();
} // toString

const char* tMessage::typeToString( )
{
  switch( eventType )
  {
    case EV_UNKNOWN:
      return "EV_UNKNOWN";
      break;
    case EV_BASE:
      return "EV_BASE";
      break;
    case EV_SCRIPT:
      return "EV_SCRIPT";
      break;
    case EV_PERL:
      return "EV_PERL";
      break;
    case EV_BIN:
      return "EV_BIN";
      break;
    case EV_URL:
      return "EV_URL";
      break;
    case EV_RESULT:
      return "EV_RESULT";
      break;
    case EV_WORKER_DONE:
      return "EV_WORKER_DONE";
      break;
    case EV_COMMAND:
      return "EV_COMMAND";
      break;
    case EV_PERSISTENT:
      return "EV_PERSISTENT";
      break;
    case EV_ERROR:
      return "EV_ERROR";
      break;
    default:
      return "unknown eventType";
  } // switch
} // typeToString

const char* tMessage::commandToString( )
{
  eCommandType command = getCommand();
  switch( command )
  {
    case CMD_NONE:
      return "CMD_NONE";
      break;
    case CMD_STATS:
      return "CMD_STATS";
      break;
    case CMD_RESET_STATS:
      return "CMD_RESET_STATS";
      break;
    case CMD_REOPEN_LOG:
      return "CMD_REOPEN_LOG";
      break;
    case CMD_REREAD_CONF:
      return "CMD_REREAD_CONF";
      break;
    case CMD_EXIT_WHEN_DONE:
      return "CMD_EXIT_WHEN_DONE";
      break;
    case CMD_TIMER_SIGNAL:
      return "CMD_TIMER_SIGNAL";
      break;
    case CMD_CHILD_SIGNAL:
      return "CMD_CHILD_SIGNAL";
      break;
    case CMD_SHUTDOWN:
      return "CMD_SHUTDOWN";
      break;
    case CMD_CONF:
      return "CMD_CONF";
      break;
    case CMD_WORKER_CONF:
      return "CMD_WORKER_CONF";
      break;
    case CMD_END_OF_QUEUE:
      return "CMD_END_OF_QUEUE";
      break;
    case CMD_PERSISTENT_APP:
      return "CMD_PERSISTENT_APP";
      break;
    case CMD_DUMP_STATE:
      return "CMD_DUMP_STATE";
      break;
    default:
      return "unknown";
  } // switch
} // commandToString
